/**
 * Created by Administrator on 2017/3/2 0002.
 */
var Game={
    create:function () {
        var game={};
        var currentIdx=0;
        var downX;
        var bool_down;
        var currentPage;
        game.ui=new PIXI.DisplayObjectContainer();

        game.ui.on('pointerdown',downHandler)
        game.ui.on('pointerup',upHandler)


        var arr_page=[];
        // for(var i=0;i<5;i++)
        // {
        //     var page=Page1.create();
        //     con.addChild(page.ui);
        //     page.ui.y=i*pixiStageH;
        //     arr_page.push(page);
        // }
        // var sc=Scroller.create(con,pixiStageW,pixiStageH,'ver','AA',arr_page);

        var arr_mc0=[];
        var arr_mc1=[];
        for(var i=1;i<14;i++)
        {
            if(i<10)
            {
                arr_mc0.push('assets/images/mc0/mc0000'+i.toString()+'.png')
            }
            else if(i<100)
            {
                arr_mc0.push('assets/images/mc0/mc000'+i.toString()+'.png')
            }
        }
        for(var i=1;i<22;i++)
        {
            if(i<10)
            {
                arr_mc1.push('assets/images/mc1/mc1000'+i.toString()+'.png')
            }
            else if(i<100)
            {
                arr_mc1.push('assets/images/mc1/mc100'+i.toString()+'.png')
            }
        }

        var mc0=MyMC.create(arr_mc0);
        mc0.id=0;
        mc0.play(10);
        game.ui.addChild(mc0.ui);
        var mc1=MyMC.create(arr_mc1);
        mc1.id=1;

        var page0=Page0.create();
        var page1=Page1.create();
        var page2=Page2.create();
        var page3=Page3.create();
        var page4=Page4.create();
        var page5=Page5.create();
        var page6=Page6.create();
        var page7=Page7.create();
        var page8=Page8.create();
        var page9=Page9.create();
        game.ui.addChild(page0.ui);

        arr_page.push(page0);
        arr_page.push(page1);
        arr_page.push(page2);
        arr_page.push(page3);
        arr_page.push(page4);
        arr_page.push(page5);
        arr_page.push(page6);
        arr_page.push(page7);
        arr_page.push(page8);

        page1.ui.y=-20;
        page2.ui.y=-20;
        page3.ui.y=-20;
        page4.ui.y=-20;
        page5.ui.y=-20;
        page6.ui.y=-20;
        page7.ui.y=-20;
        page8.ui.y=-20;

        currentPage=arr_page[0];


        document.addEventListener('MC_Complete',completeHandler);
        document.addEventListener('MC_Reverse',reverseHandler);
        document.addEventListener('Main_EnableTrue',function()
        {
            game.ui.interactive=true;
        });

        function completeHandler(e) {

            if(e.detail=='0')
            {
                mc1.play(5);
                game.ui.addChild(mc1.ui);
                currentIdx=0;
                page0.Init();
                game.ui.addChild(page0.ui);

                // currentIdx=8;
                // currentPage=arr_page[currentIdx];
                // page1.In();
                // game.ui.addChild(page1.ui);
            }
            else if(e.detail=='1')
            {
                // game.ui.interactive=true;
                currentPage.In();
                game.ui.addChild(currentPage.ui);
            }
        }
        function reverseHandler(e)
        {
            if(e.detail=='0')
            {
                // mc1.play(10);
                // game.ui.addChild(mc1.ui);
                game.ui.addChild(page9.ui);
                page9.In();
            }
            else if(e.detail=='1')
            {
                // game.ui.interactive=true;
                currentPage.In();
                game.ui.addChild(currentPage.ui);
            }
        }
        function downHandler(e) {

            bool_down=true;
            downX=e.data.global.x;
        }
        function upHandler(e) {
            if(bool_down)
            {
                console.log(e.data.global.x-downX)
                bool_down=false;
                if(e.data.global.x-downX>30)
                {
                    if(currentIdx>1)
                    {
                        game.ui.interactive=false;
                        setTimeout(function () {
                            mc1.reversePlay(15);
                        },1000);
                        currentPage.Out();
                        currentIdx--;
                        currentPage=arr_page[currentIdx];
                    }
                    else
                    {
                        game.ui.interactive=false;
                        console.log('已是首页');
                    }
                }
                else  if(e.data.global.x-downX<-30)
                {
                    if(currentIdx<8)
                    {
                        game.ui.interactive=false;
                        setTimeout(function () {
                            mc1.play(15);
                        },1000);
                        currentPage.Out();
                        currentIdx++;
                        currentPage=arr_page[currentIdx];
                    }
                    else
                    {
                        game.ui.interactive=false;
                        console.log('已是尾页')
                        game.ui.addChild(mc0.ui);
                        mc0.reversePlay(10);
                    }
                }
            }
        }

        return game;
    }

}